package com.zle.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.zle.dao.bean.Conf;
import com.zle.dao.bean.Device;
import com.zle.service.IdeviceService;

@RestController
public class DeviceController {
	
	@Autowired
	private IdeviceService deviceService;
	@Autowired
	Conf conf;
	/**
	 * 获取系统版本信息
	 * @return
	 */
	@RequestMapping(value="/product",method=RequestMethod.GET)
	public ResponseEntity<Conf> ProductInfo(){
		
		return new ResponseEntity<Conf>(conf,HttpStatus.OK);
	}
	/**
	 * 查询设备
	 * @return
	 */
	@RequestMapping(value="/device",method=RequestMethod.GET)
	public ResponseEntity<Device> goIndex(String id){
		Device dev = new Device();
		dev.setId(id);
		dev.setDevName("dev11"+UUID.randomUUID().toString());
		dev.setSn("2342-4234-4234234-ffdsf");
		return new ResponseEntity<Device>(dev,HttpStatus.OK);
	}
	/**
	 * 设备注册
	 * @param devname 设备名称
	 * @param bindCode 绑定码
	 * @param sn 设备序列号
	 * @return
	 */
	@RequestMapping(value="/register",method=RequestMethod.POST)
	public ResponseEntity<Map<String,Object>> register(String devname,String bindCode,String sn){
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("code", 0);
		if(deviceService.register(devname, bindCode, sn)){
			result.put("msg", "注册成功!");
		}else{
			result.put("msg", "注册失败!序列号不能以'0000-'开头");
		}
		
		Date date = new Date();
		SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		result.put("registerDate", dateformat.format(date));
		
		return new ResponseEntity<Map<String,Object>>(result,HttpStatus.OK);
	}
	/**
	 * 设备解绑
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/register",method=RequestMethod.DELETE)
	public ResponseEntity<Map<String,Object>> unRegister(String id){
		Map<String,Object> result = new HashMap<String,Object>();
		if(id==null || id.equals("")){
			result.put("code", 1);
			result.put("msg", "解绑失败,id不合法");
		}else{
			result.put("code", 0);
			deviceService.unRegister(id);
			result.put("msg", "解绑成功!");
		}
		return new ResponseEntity<Map<String,Object>>(result,HttpStatus.OK);
	}

}
